package cn.hx.security;

import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Component;

@Component
public class UserLoginDetailsService implements UserDetailsService {
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //模拟数据没有这个数据
        if (username.equals("none")) {
            throw new RuntimeException("用户名或者密码错误");
        }
        //模拟数据库获取数据后设置角色和资源
        UserDetails build = User.builder().username(username)
                .password(new BCryptPasswordEncoder().encode("123"))//
                .roles("USER") //设置角色，感觉意义不大
                .authorities("security:user:add", "security:user:"+username).build();//设置权限
        return build;
    }
}
